import predict  # 引用 final_predict 所在的 predict 模块
from tabulate import tabulate
import os

def analyze_code(file_path):
    # 调用 final_predict 并获取返回的类别和概率
    class_labels, predicted_probabilities = predict.final_predict(file_path)
    
    # 生成表格数据
    table_data = []
    for label, prob in zip(class_labels, predicted_probabilities):
        red_prob = f"\033[91m{prob:.6f}\033[0m"  # 将概率值设置为红色
        table_data.append([label, red_prob])

    # 使用 tabulate 输出表格
    print(tabulate(table_data, headers=["Class", "Probability"], tablefmt="grid"))

    # 找到概率最高的类别
    predicted_class_index = predicted_probabilities.argmax()
    predicted_class = class_labels[predicted_class_index]

    # 输出预测的类别和其概率
    print(f"Predicted class: {predicted_class}")
    print(f"Probability of being a {predicted_class}: \033[91m{predicted_probabilities[predicted_class_index]:.6f}\033[0m")

if __name__ == "__main__":
    file_path = input("请输入要分析的代码文件路径: ")
    analyze_code(file_path)
